<template>
  <div style="display: flex; align-items: center; height: 100%; justify-content: space-between">
    <div class="toggle">
      <el-icon @click="change()" v-if="!isCollapse">
        <Fold />
      </el-icon>
      <el-icon @click="change()" v-else>
        <Expand />
      </el-icon>
      <Breadcrumb></Breadcrumb>
    </div>
    <el-dropdown style="width: 100px; height: 100%">
      <span class="el-dropdown-link">
        <el-avatar
          shape="square"
          :size="30"
          style="border-radius: 50%; position: relative; top: 10px; right: 5px"
          :src="userStore.user.avatar"
        ></el-avatar>
        <span>{{ userStore.user.username }}</span>
        <el-icon style="margin-left: 5px">
          <ArrowDownBold />
        </el-icon>
      </span>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item @click="loginOut">退出</el-dropdown-item>
          <el-dropdown-item @click="drawer = true">更换主题</el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
    <select-theme @confirm="confirm" :theme="theme" @close="drawer = false" :drawer="drawer"></select-theme>
  </div>
</template>

<script setup>
  import SelectTheme from '../Manage/SelectTheme.vue'
  import Breadcrumb from '../Manage/Breadcrumb.vue'
  import { ref, computed } from 'vue'
  import { useRouter } from 'vue-router'
  import { useUserStore } from '@/stores/user'
  import { logOutApi } from '@/api/system/sys_user'
  import { ElMessage } from 'element-plus'

  const drawer = ref(false)
  const $emit = defineEmits(['wider'])
  const props = defineProps({
    isCollapse: {
      type: Boolean,
      default: false
    }
  })
  const change = () => {
    $emit('wider', 1)
  }

  const userStore = useUserStore()
  const router = useRouter()

  const theme = computed(() => {
    return userStore.theme
  })
  const confirm = data => {
    userStore.setTheme(data)
    ElMessage({ message: '设置成功', type: 'success' })
    drawer.value = false
  }
  const loginOut = () => {
    logOutApi().then(res => {
      if (res.code == 200) {
        userStore.logout()
        router.push('/login')
      } else {
        ElMessage({
          message: res.msg,
          type: 'error'
        })
      }
    })
  }
</script>

<style scoped>
  .el-dropdown-link:focus {
    outline: 0;
  }

  .toggle {
    display: flex;
    align-items: center;
    margin-left: 10px;
    font-size: 16px;
  }

  :deep(.el-breadcrumb__inner.is-link) {
    color: unset;
  }

  :deep(.el-dropdown) {
    color: unset;
  }
</style>
